package hyl.core.fun;

import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.List;
import java.util.Map;
import hyl.core.MyFun;

public class MyIS {
	// -------------------- 子类判断 -----------------
	public static boolean isMap(Object obj) {
		if (obj == null)
			return false;
		return obj instanceof Map;
	}

	public static boolean isArray(Object obj) {
		if (obj == null)
			return false;
		return obj instanceof Object[];
	}

	public static boolean isList(Object obj) {
		if (obj == null)
			return false;
		return obj instanceof List;
	}

	public static boolean isDouble(Object obj) {
		if (obj == null)
			return false;
		return obj instanceof Double;
	}

	public static boolean isFloat(Object obj) {
		if (obj == null)
			return false;
		return obj instanceof Float;
	}

	public static boolean isDecimal(Object obj) {
		if (obj == null)
			return false;
		return obj instanceof BigDecimal;
	}

	public static boolean isNumber(Object obj) {
		if (obj == null)
			return false;
		return obj instanceof Number;
	}

	/**
	 * 判断是否实现了比较接口
	 * 
	 * @param obj
	 * @return
	 */
	public static boolean isComparable(Object obj) {
		if (obj == null)
			return false;
		return obj instanceof Comparable;
	}

	/**
	 * 判断是否实现了等于接口
	 * 
	 * @param obj
	 * @return
	 */
	public static boolean isEqual(Object obj) {
		if (obj == null)
			return false;
		return obj instanceof Comparable;
	}

	public static boolean isStr(Object obj) {
		if (obj == null)
			return false;
		return obj instanceof String;
	}

	public static boolean isLong(Object obj) {
		if (obj == null)
			return false;
		return obj instanceof Long;
	}

	public static boolean isInt(Object obj) {
		if (obj == null)
			return false;
		return obj instanceof Integer;
	}

	public static boolean isBool(Object obj) {
		if (obj == null)
			return false;
		return obj instanceof Boolean;
	}

	public static boolean isByte(Object obj) {
		if (obj == null)
			return false;
		return obj instanceof Byte;
	}

	public static boolean isChar(Object obj) {
		if (obj == null)
			return false;
		return obj instanceof Character;
	}

	public static boolean isNumStr(final CharSequence cs) {
		if (MyFun.isEmpty(cs)) {
			return false;
		}
		final int sz = cs.length();
		int i = 0;
		int st = 0;
		for (; i < sz; i++) {
			char c = cs.charAt(i);
			if (st == 0 && c == ' ') {
				continue;
			}
			if (c == ',') {
				return false;
			}
			if (c == '-' || c == '+') {
				st = 1;
				break;
			}
			if (Character.isDigit(c)) {
				st = 2;
				break;
			}
			if (c == '.') {
				st = 4;
				break;
			} else {
				return false;
			}
		}
		if (st == 0)
			return false;
		i++;
		for (; i < sz; i++) {
			char c = cs.charAt(i);
			if (c == ',' || c == ' ') {
				continue;
			} else if (c == '-' || c == '+') { // 一定在最前面
				return false;
			} else if (Character.isDigit(c)) {
				st = st | 2;
			} else if ('.' == c) {
				if ((st & 4) == 4)
					return false;
				st = st | 4;
			} else {
				return false;
			}
		}
		if ((st & 2) != 2)
			return false;
		return true;
	}

	/**
	 *
	 * @param s1
	 * @return 奇数 整形 ,偶数 浮点型 ,0未知
	 */
	public static int get数值类型(Number s1) {
		if (s1 instanceof Byte)
			return 1;
		else if (s1 instanceof Short)
			return 3;
		else if (s1 instanceof Integer)
			return 5;
		else if (s1 instanceof Long)
			return 7;
		else if (s1 instanceof BigInteger)
			return 9;
		else if (s1 instanceof Float)
			return 2;
		else if (s1 instanceof Double)
			return 4;
		else if (s1 instanceof BigDecimal)
			return 6;
		else
			return 0;// 未知
	}
	// -------------------- 父类判断-----------------
	// obj.getClass().isAssignableFrom(Character.class);
}
